<?php

namespace App\Http\Controllers\Admin;

/**
 * 人脉管理
 */

use App\Http\Controllers\BaseController;
use App\Services\Tools\MapService;
use Illuminate\Http\Request;                //引用http请求类
use App\Exceptions\BasicException;          //引入框架异常处理类
use App\Response\Response;

class ConnectionController extends BaseController
{
    /**
     * @api {get} /connection/userConnectionList 用户人脉关系列表
     * @apiVersion 0.1.0
     * @apiName 用户人脉关系列表
     * @apiGroup 人脉管理
     *
     * @apiParam {int} user_id 用户ID 必传
     * @apiParam {int} page 请求页码  非必传（默认1）
     * @apiParam {int} per_page 分页尺寸  非必传（默认20）
     *
     * @apiError 10010 参数校验错误
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "total": 3,
    "current_page": 1,
    "last_page": 1,
    "user_info 当前用户信息": {
    "user_id": "642795",
    "user_code": 520520,
    "head_picture": "https:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/img\/b52065b06569e6d48ab1fb407e62b9a1.jpg",
    "nickname": "花里胡哨的"
    },
    "list": [{
    "id": 34,
    "user_id": 642916,
    "today_intimacy 今日增加亲密度": "1000.00",
    "today_intimacy_free 今天免费亲密度": "1000.00",
    "intimacy 累计亲密度": "9512.00",
    "type 类型（1：非CP，2：CP）": 1,
    "class_number 阶级数": 2,
    "grade_number 等级数": 11,
    "add_time": "2021-01-13 17:56:51",
    "update_time": "2021-02-19 08:19:05",
    "user_code 用户编码": 542471,
    "head_picture 头像": "https:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/img\/32f84bc71a4d72636096659b5464e4dd.jpeg",
    "nickname 昵称": "气泡用户套路啊路口咯",
    "class_icon_url 阶级图标": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/6001686a58394.png"
    }]
    }
    }
     *
     */
    public function userConnectionList(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
//                'user_id' => 'required|integer|gt:0',
                'page' => 'filled|integer|gt:0',
                'per_page' => 'filled|integer|gt:0',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $userId = (int) $request->input('user_id');
        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);

        $first = app('model_connection_relation')
            ->select('id','relation_user_id as user_id','today_intimacy','today_intimacy_free','intimacy','type','class_number','grade_number','add_time','update_time')
            ->where('user_id', $userId);
        $list = app('model_connection_relation')
            ->select('id','user_id','today_intimacy','today_intimacy_free','intimacy','type','class_number','grade_number','add_time','update_time')
            ->where('relation_user_id', $userId)
            ->union($first)
            ->orderBy('intimacy', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page)->toArray();
        $userDataMap = [];
        if (!empty($list['data'])) {
            $userIds = array_column($list['data'], 'user_id');
            $userIds[] = $userId;
            $userDataList = app('model_user_data')->whereIn('user_id', $userIds)
                ->select('user_id','user_code','head_picture','nickname')->get()->toArray();
            $userDataMap = array_column($userDataList,null,'user_id');
            $classMap = MapService::connectionClassMap();
            foreach ($list['data'] as $key => $value) {
                $list['data'][$key]['user_code'] = $userDataMap[$value['user_id']]['user_code'] ?? '';
                $list['data'][$key]['head_picture'] = $userDataMap[$value['user_id']]['head_picture'] ?? '';
                $list['data'][$key]['nickname'] = $userDataMap[$value['user_id']]['nickname'] ?? '';
                $list['data'][$key]['class_icon_url'] = $classMap[$value['type']][$value['class_number']]['icon_url'] ?? '';
            }
        }

        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'user_info' => [
                'user_id' => $userId,
                'user_code' => $userDataMap[$userId]['user_code'] ?? '',
                'head_picture' => $userDataMap[$userId]['head_picture'] ?? '',
                'nickname' => $userDataMap[$userId]['nickname'] ?? ''
            ],
            'list' => $list['data']
        ];

        return Response::sendData($result);
    }


    /**
     * @api {get} /connection/classDetail 人脉阶级详情
     * @apiVersion 0.1.0
     * @apiName 人脉阶级详情
     * @apiGroup 人脉管理
     *
     * @apiParam {number} id   阶级ID，非必传（当不传时，返回一些配置信息）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "detail 详情": {
    "id": 6,
    "title": "生死之交",
    "icon_url": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/6001689d5dba0.png",
    "class_number": 5,
    "max_grade_number": 41,
    "day_intimacy_max": 12000,
    "day_intimacy_free_max": 4000,
    "type": 1,
    "product_id": 0,
    "intimacy_addition": 1.5,
    "intimacy_increase_rate": 6,
    "diy_nickname": 1,
    "online_notice": 1,
    "joinroom_notice": 1,
    "mall_config": {
    "1": [{
    "product_id": 299,
    "days": 10
    }, {
    "product_id": 300,
    "days": 10
    }],
    "4": [{
    "product_id": 298,
    "days": 10
    }]
    },
    "status": 1,
    "add_time": null,
    "update_time": "2021-01-25 15:46:57"
    },
    "head_frame_list 头像框列表": [{
    "id": 1,
    "title": "子爵",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/c0de0f8aea46a81f770c29f4d5bd8f67C3PKL.png"
    }, {
    "id": 2,
    "title": "伯爵",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/2b40590558c0c6e704b222d32f7c0e7dstSsE.png"
    }],
    "special_effects_list 进场特效列表": [{
    "id": 85,
    "title": "入场特效1",
    "picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5f881f3d34d59.png"
    }, {
    "id": 86,
    "title": "普天同庆",
    "picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5f89876bcf246.png"
    }],
    "prop_list 道具列表": [{
    "id": 269,
    "title": "亲密卡",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/febca25646042c3314159f4bf725c032V6dV0.png"
    }, {
    "id": 270,
    "title": "钻戒",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600e572558ad8.png"
    }]
    }
    }
     *
     */
    public function classDetail(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id' => 'filled|integer|gt:0'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $request->input('id');
        $detail = [];
        if ($id) {
            $detail = app('model_connection_class_config')
                ->where('status', '<', 2)
                ->where('id', $id)->first();
            if (empty($detail)) throw new BasicException(70101);
            $detail = $detail->toArray();
            $detail['mall_config'] = json_decode($detail['mall_config'], true);
        }
        $headFrameList = app('model_mall_products')
            ->where('category_id', 1)
            ->select('id','title','picture')
            ->get()->toArray();
        $specialEffectsList = app('model_mall_products')
            ->where('category_id', 4)
            ->select('id','title','picture')
            ->get()->toArray();
        $propList = app('model_mall_products')
            ->where('category_id', 5)
            ->select('id','title','picture')
            ->get()->toArray();
        $result = [
            'detail' => $detail,
            'head_frame_list' => $headFrameList,
            'special_effects_list' => $specialEffectsList,
            'prop_list' => $propList
        ];

        return Response::sendData($result);
    }

    /**
     * @api {post} /connection/classSave 人脉阶段保存
     * @apiVersion 0.1.0
     * @apiName 人脉阶段保存
     * @apiGroup 人脉管理
     *
     * @apiParam {number} id   记录ID，非必传（不传为新增，传则为编辑）
     * @apiParam {string} title    标题，非必传
     * @apiParam {string} icon_url    图标，必传
     * @apiParam {number} class_number     阶级数，必传
     * @apiParam {number} max_grade_number     最高等级数，必传
     * @apiParam {number} day_intimacy_max     每日亲密度上限，必传
     * @apiParam {number} day_intimacy_free_max     每日亲密度非普通礼物上限值，必传
     * @apiParam {number} type     类型，必传（1：非CP，2：CP）
     * @apiParam {number} product_id     道具ID，非必传
     * @apiParam {number} intimacy_addition     送礼物亲密度加成比例，必传
     * @apiParam {number} intimacy_increase_rate     语聊每分钟亲密度增加值，必传
     * @apiParam {number} diy_nickname     是否开启自定义称谓，必传（0：否，1：是）
     * @apiParam {number} online_notice     是否开启上线通知，必传（0：否，1：是）
     * @apiParam {number} joinroom_notice     是否开启入场通知，必传（0：否，1：是）
     * @apiParam {string} mall_config     个性商城配置，非必传（例：{"1":[{"product_id":299,"days":10},{"product_id":300,"days":10}],"4":[{"product_id":298,"days":10}]}）
     * @apiParam {number} status     状态，必传（0：不展示，1：展示）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function classSave(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id' => 'numeric|gt:0',
                'title' => 'string|max:50',
                'icon_url' => 'required|string|max:200',
                'class_number' => 'required|numeric|gte:0',
                'max_grade_number' => 'required|numeric|gte:0',
                'day_intimacy_max' => 'required|numeric|gt:0',
                'day_intimacy_free_max' => 'required|numeric|gt:0',
                'type' => 'required|numeric|in:1,2',
                'product_id' => 'numeric|gt:0',
                'intimacy_addition' => 'required|numeric|gt:0',
                'intimacy_increase_rate' => 'required|numeric|gt:0',
                'diy_nickname' => 'required|numeric|in:0,1',
                'online_notice' => 'required|numeric|in:0,1',
                'joinroom_notice' => 'required|numeric|in:0,1',
                'mall_config' => 'string',
                'status' => 'required|numeric|in:0,1',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $input = $request->input();
        $data = [
            'title' => $input['title'] ?? '',
            'icon_url' => $input['icon_url'],
            'class_number' => $input['class_number'],
            'max_grade_number' => $input['max_grade_number'],
            'day_intimacy_max' => $input['day_intimacy_max'],
            'day_intimacy_free_max' => $input['day_intimacy_free_max'],
            'type' => $input['type'],
            'product_id' => isset($input['product_id'])&&$input['product_id'] ? $input['product_id'] : 0,
            'intimacy_addition' => $input['intimacy_addition'],
            'intimacy_increase_rate' => $input['intimacy_increase_rate'],
            'diy_nickname' => $input['diy_nickname'],
            'online_notice' => $input['online_notice'],
            'joinroom_notice' => $input['joinroom_notice'],
            'mall_config' => $input['mall_config'] ?? null,
            'status' => $input['status']
        ];
        $id = $request->input('id', 0);
        if ($id) {
            $detail = app('model_connection_class_config')
                ->where('status', '<', 2)
                ->where('id', $id)->first();
            if (empty($detail)) throw new BasicException(70101);
            app('model_connection_class_config')->where('id', $id)->update($data);
        } else {
            $data['add_time'] = date('Y-m-d H:i:s');
            app('model_connection_class_config')->insert($data);
        }

        return Response::sendData([]);
    }

    /**
     * @api {post} /connection/classDelete 人脉阶级删除
     * @apiVersion 0.1.0
     * @apiName 人脉阶级删除
     * @apiGroup 人脉管理
     *
     * @apiParam {string} ids   记录ID，必传（多个则以英文逗号隔开）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70102 符合删除记录数量不足
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function classDelete(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $ids = $request->input('ids');
        $ids = explode(',', $ids);
        $count = app('model_connection_class_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->count();
        if (count($ids) > $count) throw new BasicException(70102);
        app('model_connection_class_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->update([
                'status' => 2
            ]);

        return Response::sendData([]);
    }

    /**
     * @api {get} /connection/classList 人脉阶级列表
     * @apiVersion 0.1.0
     * @apiName 人脉阶级列表
     * @apiGroup 人脉管理
     *
     * @apiParam {int} page 请求页码  非必传（默认1）
     * @apiParam {int} per_page 分页尺寸  非必传（默认20）
     *
     * @apiError 10010 参数校验错误
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "total 总记录数": 12,
    "current_page 当前页码数": 1,
    "last_page 最后一页页码数": 1,
    "list": [{
    "id": 12,
    "title 标题": "三生三世",
    "icon_url 图标": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600168e15bed0.png",
    "class_number 阶级数": 5,
    "max_grade_number 升阶最低等级数": 41,
    "day_intimacy_max 每日亲密度上限": 12000,
    "day_intimacy_free_max 每日免费亲密度上限": 4000,
    "type 类型 1：非CP，2：CP": 2,
    "product_id 道具ID": 0,
    "intimacy_addition 送礼亲密度加成比例": 1.5,
    "intimacy_increase_rate 语聊亲密度增加速度": 6,
    "diy_nickname 是否开启自定义称谓（0：否，1：是）": 1,
    "online_notice 是否开启上线通知（0：否，1：是）": 1,
    "joinroom_notice 是否开启入场通知（0：否，1：是）": 1,
    "mall_config 个性商城配置（1：头像框，4：入场特效）": {
    "1": [{
    "product_id": 301
    }, {
    "product_id": 302
    }],
    "4": [{
    "product_id": 297
    }]
    },
    "status 状态（0：不展示，1：展示）": 1,
    "add_time": null,
    "update_time": "2021-01-25 15:48:28"
    }, {
    "id": 11,
    "title": " 海誓山盟",
    "icon_url": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600168d400725.png",
    "class_number": 4,
    "max_grade_number": 31,
    "day_intimacy_max": 6000,
    "day_intimacy_free_max": 2000,
    "type": 2,
    "product_id": 270,
    "intimacy_addition": 1.3,
    "intimacy_increase_rate": 4,
    "diy_nickname": 1,
    "online_notice": 1,
    "joinroom_notice": 1,
    "mall_config": {
    "1": [{
    "product_id": 301
    }, {
    "product_id": 302
    }]
    },
    "status": 1,
    "add_time": null,
    "update_time": "2021-01-25 15:48:25"
    }],
    "head_frame_list 头像框列表": [{
    "id": 1,
    "title": "子爵",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/c0de0f8aea46a81f770c29f4d5bd8f67C3PKL.png"
    }, {
    "id": 2,
    "title": "伯爵",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/2b40590558c0c6e704b222d32f7c0e7dstSsE.png"
    }],
    "special_effects_list 入场特效列表": [{
    "id": 85,
    "title": "入场特效1",
    "picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5f881f3d34d59.png"
    }, {
    "id": 86,
    "title": "普天同庆",
    "picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5f89876bcf246.png"
    }],
    "prop_list 道具列表": [{
    "id": 269,
    "title": "亲密卡",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/febca25646042c3314159f4bf725c032V6dV0.png"
    }, {
    "id": 270,
    "title": "钻戒",
    "picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600e572558ad8.png"
    }]
    }
    }
     *
     */
    public function classList(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'page' => 'filled|integer|gt:0',
                'per_page' => 'filled|integer|gt:0',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $list = app('model_connection_class_config')
            ->whereIn('status', [0,1])
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page)->toArray();
        if (!empty($list['data'])) {
            foreach ($list['data'] as $key => $value) {
                $list['data'][$key]['mall_config'] = json_decode($value['mall_config'], true);
            }
        }

        $headFrameList = app('model_mall_products')
            ->where('category_id', 1)
            ->select('id','title','picture')
            ->get()->toArray();
        $specialEffectsList = app('model_mall_products')
            ->where('category_id', 4)
            ->select('id','title','picture')
            ->get()->toArray();
        $propList = app('model_mall_products')
            ->where('category_id', 5)
            ->select('id','title','picture')
            ->get()->toArray();

        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $list['data'],
            'head_frame_list' => $headFrameList,
            'special_effects_list' => $specialEffectsList,
            'prop_list' => $propList
        ];

        return Response::sendData($result);
    }

    /**
     * @api {get} /connection/gradeDetail 人脉等级详情
     * @apiVersion 0.1.0
     * @apiName 人脉等级详情
     * @apiGroup 人脉管理
     *
     * @apiParam {number} id   等级ID 必传
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "id": 1,
    "title": "等级1",
    "number 等级数": 1,
    "exp 最低经验值": 0,
    "status 状态 0：不展示，1：展示，2：已删除": 1,
    "add_time": "2021-01-04 16:53:09",
    "update_time": null
    }
    }
     *
     */
    public function gradeDetail(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id'    => 'required|integer|gt:0'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $request->input('id');
        $detail = app('model_connection_grade_config')->where('id', $id)->first();
        if (empty($detail)) throw new BasicException(70101);

        return Response::sendData($detail->toArray());
    }

    /**
     * @api {post} /connection/gradeSave 人脉等级保存
     * @apiVersion 0.1.0
     * @apiName 人脉等级保存
     * @apiGroup 人脉管理
     *
     * @apiParam {number} id   记录ID，非必传（不传为新增，传则为编辑）
     * @apiParam {string} title    标题，非必传
     * @apiParam {number} number     等级数，必传
     * @apiParam {number} exp     所需最低经验值，必传
     * @apiParam {number} status     状态，必传（0：不展示，1：展示）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function gradeSave(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id' => 'numeric|gt:0',
                'title' => 'string|max:50',
                'number' => 'required|integer|min:1',
                'exp' => 'required|integer|min:0',
                'status' => 'required|integer|in:0,1',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $data = [
            'title' => $request->input('title', ''),
            'number' => $request->input('number'),
            'exp' => $request->input('exp', 0),
            'status' => $request->input('status')
        ];
        $id = $request->input('id', 0);
        if ($id) {
            $detail = app('model_connection_grade_config')->where('id', $id)->first();
            if (empty($detail)) throw new BasicException(70101);
            app('model_connection_grade_config')->where('id', $id)->update($data);
        } else {
            $data['add_time'] = date('Y-m-d H:i:s');
            app('model_connection_grade_config')->insert($data);
        }

        return Response::sendData([]);
    }

    /**
     * @api {post} /connection/gradeDelete 人脉等级删除
     * @apiVersion 0.1.0
     * @apiName 人脉等级删除
     * @apiGroup 人脉管理
     *
     * @apiParam {string} ids   记录ID，必传（多个则以英文逗号隔开）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70102 符合删除记录数量不足
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function gradeDelete(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $ids = $request->input('ids');
        $ids = explode(',', $ids);
        $count = app('model_connection_grade_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->count();
        if (count($ids) > $count) throw new BasicException(70102);
        app('model_connection_grade_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->update([
                'status' => 2
            ]);

        return Response::sendData([]);
    }

    /**
     * @api {get} /connection/gradeList 人脉等级列表
     * @apiVersion 0.1.0
     * @apiName 人脉等级列表
     * @apiGroup 人脉管理
     *
     * @apiParam {int} page 请求页码  非必传（默认1）
     * @apiParam {int} per_page 分页尺寸  非必传（默认20）
     *
     * @apiError 10010 参数校验错误
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "total": 50,
    "current_page": 1,
    "last_page": 3,
    "list": [{
    "id": 50,
    "title": "等级50",
    "number": 50,
    "exp": 1000000,
    "status": 1,
    "add_time": "2021-01-04 16:53:09",
    "update_time": "2021-01-19 10:18:47"
    }, {
    "id": 49,
    "title": "等级49",
    "number": 49,
    "exp": 911840,
    "status": 1,
    "add_time": "2021-01-04 16:53:09",
    "update_time": "2021-01-19 10:18:47"
    }]
    }
    }
     *
     */
    public function gradeList(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'page' => 'filled|integer|gt:0',
                'per_page' => 'filled|integer|gt:0',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $list = app('model_connection_grade_config')
            ->whereIn('status', [0,1])
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page)->toArray();
        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $list['data']
        ];

        return Response::sendData($result);
    }
}
